//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "StockReport.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
__fastcall TfrmStockReport::TfrmStockReport(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   stockReport = new TStockReport( dbMain);
   total.remain = 0;
   total.cost   = 0;
}
//---------------------------------------------------------------------------

void __fastcall TfrmStockReport::FormDestroy(TObject *Sender)
{
   delete stockReport;
}

//---------------------------------------------------------------------------
void TfrmStockReport :: Preview( __int64 firmID, AnsiString model)
{
   TQuery * query;

   stockReport->Produce( firmID, model);

   try
   {
      query = stockReport->GetQuery();
      qrStock->DataSet = query;
      bndGroup->Expression = "NAME";
      txtFirm->DataSet = query;
      txtFirm->DataField = "NAME";
      txtModel->DataSet = query;
      txtModel->DataField = "MODEL";
      txtPrice->DataSet = query;
      txtPrice->DataField = "PRICE";
      txtDiff->DataSet = query;
      txtDiff->DataField = "DIFF";
      txtCost->DataSet = query;
      txtCost->DataField = "COST";

      qrStock->Preview();
   }
   __finally
   {
      stockReport->Close();
   }
}

//--------------------------TStockReport-------------------------------------

TStockReport :: TStockReport( TDatabase * database)
{
   qryModels = new TQuery( NULL);
   qryModels->DatabaseName = database->DatabaseName;
   BuildQuery();
}

TStockReport :: ~TStockReport()
{
   delete qryModels;
}

void TStockReport :: BuildQuery()
{
   qryModels->SQL->Add( "SELECT NAME, MODEL, PRICE, ( BOUGHT - SOLD) AS DIFF, (( BOUGHT - SOLD) * PRICE) AS COST FROM GLASSESFIRMS, GLASSESMODELS ");
   qryModels->SQL->Add( "WHERE ( GLASSESFIRMS.ID = GLASSESMODELS.FIRMID)AND ");
   qryModels->SQL->Add( "(( :prmFirmID = -1) OR ( FIRMID = :prmFirmID)) AND ");
   qryModels->SQL->Add( "(( :prmModel = '-1') OR ( MODEL = :prmModel)) ");
   qryModels->SQL->Add( "ORDER BY NAME, MODEL");
}

TQuery * TStockReport :: GetQuery()
{
   return qryModels;
}

void TStockReport :: Produce( __int64 firmID, AnsiString model)
{
   qryModels->ParamByName( "prmFirmID")->AsInteger = firmID;
   qryModels->ParamByName( "prmModel")->AsString = model;

   qryModels->Open();
}

void TStockReport :: Close()
{
   qryModels->Close();
}



void __fastcall TfrmStockReport::txtDiffPrint(TObject *sender,
      AnsiString &Value)
{
   TQuery * query;

   query = ( TQuery*)qrStock->DataSet;
   total.remain += query->FieldByName( txtDiff->DataField)->AsInteger;
}
//---------------------------------------------------------------------------

void __fastcall TfrmStockReport::txtCostPrint(TObject *sender,
      AnsiString &Value)
{
   TQuery * query;

   query = ( TQuery*)qrStock->DataSet;
   total.cost += ( double)query->FieldByName( txtCost->DataField)->AsCurrency;
}
//---------------------------------------------------------------------------

void __fastcall TfrmStockReport::bndSummaryBeforePrint(
      TQRCustomBand *Sender, bool &PrintBand)
{
   lblTotalRemain->Caption = total.remain;
   lblRemainCost->Caption  = total.cost;
   lblRemainCost->Caption  = CurrencyString + " " + lblRemainCost->Caption;

   /*Clear after summary is printed, each printing causes again all events*/
   total.remain = 0;
   total.cost   = 0;
}


